using System;
using System.Collections;
using System.Configuration;
using System.Xml;
using System.Collections.Generic;


namespace Phoenix.Cache
{
    /// <summary>
    /// config file provider
    /// </summary>
    public class MemCacheSectionHandler : IConfigurationSectionHandler
    {

        /// <summary>
        /// parse the config section
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="configContext"></param>
        /// <param name="section"></param>
        /// <returns>an array of <see cref="MemCacheConfig"/> objects</returns>
        public object Create(object parent, object configContext, XmlNode section)
        {

            string sectionContent = section.OuterXml;

            if (string.IsNullOrEmpty(sectionContent))
            {
                return null;
            }

            ArrayList configs = new ArrayList();

            foreach (XmlNode childNode in section.ChildNodes)
            {
                string host = childNode.Attributes["host"].Value;
                int port = int.Parse(childNode.Attributes["port"].Value);
                int weight =int.Parse(childNode.Attributes["weight"].Value);
                configs.Add(new MemcachedServer(host, port, weight));
                
            }
            return configs.ToArray(typeof(MemcachedServer));

        }
    }
}
